package task4;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class UserClient {
    public static void main(String[] args) {
        Socket socket = null;
        UserMessage um = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            socket = new Socket("127.0.0.1", 8888);
            um = new UserMessage("check", new User("admin", "123456"));
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(um);
            ois = new ObjectInputStream(socket.getInputStream());
            um = (UserMessage)ois.readObject();
            if("success".equals(um.getType())){
                System.out.println("登陆成功");
            } else{
                System.out.println("登陆失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(null == ois){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null == oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null == socket){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
